package cn.itcast.algorithm.exercise;

import cn.itcast.algorithm.exercise.helpclass.ListNode;
import org.junit.Test;

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class ExerciseAB24 {
    @Test
    public void exerciseAB12Test() {
        int[] nums = new int[]{-1, 1} ;
        int target = 0 ;
        System.out.println(search(nums, target));
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     * @param nums int整型一维数组
     * @param target int整型
     * @return int整型
     */
    public int search (int[] nums, int target) {
        // write code here
        if (nums == null || nums.length == 0) {
            return -1 ;
        }

        int arrLength = nums.length;
        int start = 0 ;
        int end = arrLength - 1 ;

        while(start <= end) {
            int mid = start + (end - start) / 2 ;

            if (target > nums[mid]) {
                start = mid + 1 ;
            }else if(target < nums[mid]) {
                end = mid - 1 ;
            }else {
                return mid ;
            }
        }

        return -1 ;
    }
}
